<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <!--<el-col class="other">-->
          <!--<el-form-item label="部门名称" size="small">-->
            <!--<el-input v-model="searchForm.teamName"></el-input>-->
          <!--</el-form-item>-->
        <!--</el-col>-->
        <el-col class="singleDatePicker">
          <el-form-item label="结算月份" size="small">
            <el-date-picker
              v-model="searchForm.finalMonth"
              type="month"
              value-format="timestamp"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-download" @click="downloadFile">导入</el-button>
      <input class="file" type="file" @change="fileChanged" ref="file"/>
    </div>

    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="80"
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.propertyId)"
            type="text"
            size="small">
            修改
          </el-button>
          <!--
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
          -->
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="650px">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
        <el-row>
          <el-col class="other">
            <el-form-item label="部门名称" size="small">
              <el-input type="text" v-model="teamRow.teamName" disabled></el-input>
            </el-form-item>
            <el-form-item label="水费" size="small">
              <el-input type="number" v-model.number="teamRow.waterFee">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="照明费" size="small">
              <el-input type="number" v-model.number="teamRow.illuminationFee">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="房租" size="small">
              <el-input type="number" v-model.number="teamRow.rentFee">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="能耗费" size="small">
              <el-input type="number" v-model.number="teamRow.energyFee">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="固定资产" size="small">
              <el-input type="number" v-model.number="teamRow.fixedAssets">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="办公小计" size="small">
              {{workSubtotal}}元
              <!--<el-input type="number" v-model.number="teamRow.workSubtotal">-->
              <!--<span slot="suffix">元</span>-->
              <!--</el-input>-->
            </el-form-item>
            <el-form-item label="合计" size="small">
              {{propertyTotal}}元
            </el-form-item>
          </el-col>
          <el-col class="other">
            <el-form-item label="结算月份" size="small">
              <el-date-picker
                v-model="teamRow.finalMonth"
                type="month"
                value-format="timestamp"
                placeholder="选择日期"
                disabled
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="空调费" size="small">
              <el-input type="number" v-model.number="teamRow.vrvFee">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="公摊费" size="small">
              <el-input type="number" v-model.number="teamRow.pooled">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="物业费" size="small">
              <el-input type="number" v-model.number="teamRow.tenementFee">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="物业小计" size="small">
              {{tenementSubtotal}}元
            </el-form-item>
            <el-form-item label="易耗品" size="small">
              <el-input type="number" v-model.number="teamRow.consumable">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
            <el-form-item label="其他" size="small">
              <el-input type="number" v-model.number="teamRow.other">
                <span slot="suffix">元</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    selectDepartmentAdministrativeFee,
    updateDepartmentAdministrativeFee,
    batchUpdateInfo
  } from '@/api/propertycharge'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const configs = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    filters: {},
    template: `
  <el-table-column :label="colConfig.label">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width" :show-overflow-tooltip="true">
                <span slot-scope="scope" >
                    <span>{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  const finalMonth = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width"  :fixed="colConfig.fixed">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'finalMonth', label: '结算月份', width: '100', fixed: true, component: finalMonth },
          { prop: 'teamName', label: '部门', width: '150', fixed: true },
          {
            label: '物业费用', children: [
              { prop: 'waterFee', label: '水费(元)', width: '100' },
              { prop: 'vrvFee', label: '空调费(元)', width: '100' },
              { prop: 'illuminationFee', label: '照明费(元)', width: '100' },
              { prop: 'pooled', label: '公摊费(元)', width: '100' },
              { prop: 'rentFee', label: '房租(元)', width: '100' },
              { prop: 'tenementFee', label: '物业费(元)', width: '100' },
              { prop: 'energyFee', label: '能耗费(元)', width: '100' },
              { prop: 'tenementSubtotal', label: '小计(元)', width: '100' }
            ], component: configs
          },
          {
            label: '办公费用', children: [
              { prop: 'fixedAssets', label: '固定资产(元)', width: '100' },
              { prop: 'consumable', label: '易耗品(元)', width: '100' },
              { prop: 'workSubtotal', label: '小计(元)', width: '100' }
            ], component: configs
          },
          { prop: 'other', label: '其他', width: '80' },
          { prop: 'total', label: '合计', width: '80' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {},
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      teamId() {
        return this.$store.state.user.teamId
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      propertyTotal() {
        const propertyTotal = this.tenementSubtotal + this.workSubtotal + this.teamRow.other
        return Math.round(propertyTotal * 100) / 100
      },
      tenementSubtotal() {
        const tenementSubtotal = this.teamRow.waterFee + this.teamRow.vrvFee + this.teamRow.illuminationFee + this.teamRow.pooled + this.teamRow.rentFee + this.teamRow.tenementFee + this.teamRow.energyFee
        return Math.round(tenementSubtotal * 100) / 100
      },
      workSubtotal() {
        const workSubtotal = this.teamRow.fixedAssets + this.teamRow.consumable
        return Math.round(workSubtotal * 100) / 100
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      editTeam(propertyId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改团队月费用表'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          propertyId: propertyId
        }
        selectDepartmentAdministrativeFee(params).then(response => {
          this.teamRow = Object.assign({}, this.teamRow, response.responseBody)
        })
      },
      submitForm(formName) {
        const params = {
          waterFee: this.teamRow.waterFee || 0,
          vrvFee: this.teamRow.vrvFee || 0,
          illuminationFee: this.teamRow.illuminationFee || 0,
          pooled: this.teamRow.pooled || 0,
          rentFee: this.teamRow.rentFee || 0,
          tenementFee: this.teamRow.tenementFee || 0,
          energyFee: this.teamRow.energyFee || 0,
          tenementSubtotal: this.tenementSubtotal,
          fixedAssets: this.teamRow.fixedAssets || 0,
          consumable: this.teamRow.consumable || 0,
          workSubtotal: this.workSubtotal,
          other: this.teamRow.other || 0,
          total: this.propertyTotal
        }
        if (!this.dialogType) {
          params.propertyId = this.teamRow.propertyId
        }
        if (this.teamRow.teamId) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if (this.dialogType) {
                addTenementDetail(params).then(response => {
                  this.outerVisible = false
                  this.fetchData()
                })
              } else {
                updateDepartmentAdministrativeFee(params).then(response => {
                  this.outerVisible = false
                  this.fetchData()
                })
              }
            } else {
              console.log('error submit!!')
              return false
            }
          })
        } else {
          this.$message({
            message: '该部门不存在',
            type: 'warning'
          })
        }
      },
      downloadFile() {
        this.$refs.file.click()
      },
      fileChanged() {
        const excel = this.$refs.file.value
        const params = new FormData()
        console.log(this.$refs.file.value)
        var reg = /^.*\.(?:xls|xlsx)$/i
        if (!reg.test(excel)) {
          this.$message({
            message: '请上传excel格式的文件!',
            type: 'error',
            duration: 5 * 1000
          })
          return
        } else {
          params.append('file', this.$refs.file.files[0])
          batchUpdateInfo(params).then(response => {
            this.fetchData()
          })
          if (this.$refs.file) {
            this.$refs.file.value = ''
          }
        }
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .file {
      display: none;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
